In [1]:
# ! /usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import getpass
In [2]:
def user():
# ORG_EMAIL = "@gmail.com"
# FROM_EMAIL = "ypur mail" + ORG_EMAIL
# FROM_PWD = "yourpss"
FROM_EMAIL = raw_input("insert Email : ")
FROM_PWD = getpass.getpass("input : ")
return FROM_EMAIL,FROM_PWD
In [3]:
def login():
gmail_user, gmail_pwd = user() #calling the user function for get user details
smtpserver = smtplib.SMTP("smtp.gmail.com",587) #Declaring gmail SMTP server address and port
smtpserver.starttls() #Starting tls service, Transport Layer Security (TLS) are cryptographic protocols that provide communications security over a computer network.
smtpserver.login(gmail_user, gmail_pwd) #Login to Gmail server using TLS
print 'Login successful'
return smtpserver
In [2]:
# text = "Hi!\n5633222222222222222http://www.python.org"
# html = """\
# <html>
# <head></head>
# <body>
# <p>Hi!<br>
# How are you?<br>
# Here is the <a href="http://www.python.org">link</a> you wanted.
# </p>
# </body>
# </html>
# """
def Send_Mail(smtpserver,TO_EMAIL,text=None,html=None,subject='Subject missing',FROM_EMAIL='Shahariar'):
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative') # In turn, use text/plain and text/html parts within the multipart/alternative part.
msg['Subject'] = subject #Subject of the message
msg['From'] = formataddr((str(Header(FROM_EMAIL, 'utf-8')), FROM_EMAIL)) #Adding custom Sender Name
msg['To'] = TO_EMAIL #Assining Reciver email
part1 = MIMEText(text, 'plain') #adding text part of mail
part2 = MIMEText(html, 'html') #Adding HTMLpart of mail
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1) #attach Plain text
msg.attach(part2) #attach HTML text
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
try:
smtpserver.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
print " Message Send"
smtpserver.quit() #stopping server
except Exception:
print Exception